package com.java.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.java.control.search.ConditionPart;
import com.java.control.search.SearchCond;
import com.java.entity.Supplier;
import com.java.entity.SupplierQuotation;

/**
 * 供应商管理服务
 * @author wqf
 * @version  Nov 24, 2011 11:51:06 AM
 */
@Transactional(propagation = Propagation.REQUIRED,readOnly=false, rollbackFor = Exception.class)
public class SupplierService extends BaseService{

	/**
	 * 添加供应商
	 * @param supplier
	 */
	public void addSupplier(Supplier supplier){
		this.dao.saveObject(supplier);
	}
	
	/**
	 * 编辑供应商（for dwr）
	 * @param supplier
	 */
	public void editSupplier(Supplier supplier){
		if(null == supplier.getId()){
			this.addSupplier(supplier);
		}else{
			this.updateSupplier(supplier);
		}
	}	
	
	/**
	 * 修改供应商
	 * @param supplier
	 */
	public void updateSupplier(Supplier supplier){
		this.dao.updateObject(supplier);
	}
	
	/**
	 * 删除供应商
	 * @param id
	 */
	public void deleteSupplier(Long id){
		Supplier supplier = this.dao.loadObject(Supplier.class, id);
		if(supplier!=null){
			List<SupplierQuotation> list = this.dao.loadObjects(SupplierQuotation.class, "supplier.id=?", null, null, null, supplier.getId());
			for(SupplierQuotation entity:list){
				this.dao.deleteObject(entity);
			}
			this.dao.deleteObject(supplier);			
		}
	}

	public Supplier getSupplierById(Long id){
		return this.dao.loadObject(Supplier.class, id);
	}
	
	public List<Supplier> getSupplierByIds(String ids){
		if(StringUtils.isNotBlank(ids)){
			return this.dao.loadObjects(Supplier.class, "id in(" + ids + ")", "id desc", null, null);			
		}
		return null;
	}
	
	public List<Supplier> getSupplierList(SearchCond searchCond){
		ConditionPart condPart = searchCond.getCondition();
		Object params[]=condPart.getParams().toArray(new Object[0]);
		List<Supplier> list=this.dao.loadObjects(Supplier.class,condPart.getExpression(), "id", searchCond.getPageSize(), searchCond.getPageIndex(), params);
		return list;
	}
	public void deleteSupplierByIds(String ids){
		if(StringUtils.isNotBlank(ids)){
			String []id  = ids.split(",");
			for(String s:id){
				deleteSupplier(Long.parseLong(s));
			}			
		}	
	}
}
